﻿/*  Copyright (c) 2012 William Rogers and James Boud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using SlimDX;
using SlimDX.Direct3D11;

namespace JBBRXG11
{
    /// <summary>
    /// Contains sampler state, which determines how to sample texture data.
    /// </summary>
    public class SamplerState : IDisposable
    {
        /// <summary>
        /// Contains default state for anisotropic filtering and texture coordinate clamping.
        /// </summary>
        public static readonly SamplerState AnisotropicClamp;
        /// <summary>
        /// Contains default state for anisotropic filtering and texture coordinate wrapping.
        /// </summary>
        public static readonly SamplerState AnisotropicWrap;
        /// <summary>
        /// Contains default state for linear filtering and texture coordinate clamping.
        /// </summary>
        public static readonly SamplerState LinearClamp;
        /// <summary>
        /// Contains default state for linear filtering and texture coordinate wrapping.
        /// </summary>
        public static readonly SamplerState LinearWrap;
        /// <summary>
        /// Contains default state for point filtering and texture coordinate clamping.
        /// </summary>
        public static readonly SamplerState PointClamp;
        /// <summary>
        /// Contains default state for point filtering and texture coordinate wrapping.
        /// </summary>
        public static readonly SamplerState PointWrap;

        static SamplerState()  // TODO - check default
        {
            AnisotropicClamp = new SamplerState();
            AnisotropicClamp.Filter = Microsoft.Xna.Framework.Graphics.TextureFilter.Anisotropic;

            AnisotropicWrap = new SamplerState();
            AnisotropicWrap.Filter = Microsoft.Xna.Framework.Graphics.TextureFilter.Anisotropic;
            AnisotropicWrap.AddressU = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;
            AnisotropicWrap.AddressV = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;
            AnisotropicWrap.AddressW = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;

            LinearClamp = new SamplerState();
            LinearClamp.Filter = Microsoft.Xna.Framework.Graphics.TextureFilter.Linear;

            LinearWrap = new SamplerState();
            LinearWrap.Filter = Microsoft.Xna.Framework.Graphics.TextureFilter.Linear;
            LinearWrap.AddressU = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;
            LinearWrap.AddressV = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;
            LinearWrap.AddressW = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;

            PointClamp = new SamplerState();
            PointClamp.Filter = Microsoft.Xna.Framework.Graphics.TextureFilter.Point;

            PointWrap = new SamplerState();
            PointWrap.Filter = Microsoft.Xna.Framework.Graphics.TextureFilter.Point;
            PointWrap.AddressU = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;
            PointWrap.AddressV = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;
            PointWrap.AddressW = Microsoft.Xna.Framework.Graphics.TextureAddressMode.Wrap;
        }

        SamplerDescription ssdesc;
        SlimDX.Direct3D11.SamplerState dxsamplerstate;

        internal SlimDX.Direct3D11.SamplerState Get_DX_SamplerState(Device dx_dev)
        {
            if (dxsamplerstate == null)
                dxsamplerstate = SlimDX.Direct3D11.SamplerState.FromDescription(dx_dev, ssdesc);
            return dxsamplerstate;
        }

        private void modified()
        {
            if (dxsamplerstate != null)
            {
                dxsamplerstate.Dispose();
                dxsamplerstate = null;
            }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Constructors ////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public SamplerState()
        {
            ssdesc = new SamplerDescription();
            ssdesc.AddressU = TextureAddressMode.Clamp;
            ssdesc.AddressV = TextureAddressMode.Clamp;
            ssdesc.AddressW = TextureAddressMode.Clamp;
            ssdesc.BorderColor = new Color4(1.0f, 0.0f, 0.0f, 0.0f);
            ssdesc.ComparisonFunction = Comparison.Always;
            ssdesc.Filter = SlimDX.Direct3D11.Filter.MinMagMipPoint;
            ssdesc.MaximumAnisotropy = 4;
            ssdesc.MaximumLod = 0;
            ssdesc.MinimumLod = 0;
            ssdesc.MipLodBias = 0;
            dxsamplerstate = null;
            GraphicsDeviceManager.GetGame(Game.GraphicsDevice).UserContentAdd(this);
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Properties //////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Gets or sets the mipmap LOD bias. The default value is 0. A negative
        /// value indicates a larger mipmap level; a positive value indicates a
        /// smaller mipmap level.
        /// </summary>
        public float MipMapLevelOfDetailBias { get { return ssdesc.MipLodBias; } set { ssdesc.MipLodBias = value; modified(); } }

        /// <summary>
        /// Gets or sets the level of detail (LOD) index of the largest map to use.
        /// </summary>
        public int MaxMipLevel { get { return (int)ssdesc.MaximumLod; } set { ssdesc.MaximumLod = value; modified(); } }

        /// <summary>
        /// Gets or sets the maximum anisotropy. The default value is 4.
        /// </summary>
        public int MaxAnisotropy { get { return ssdesc.MaximumAnisotropy; } set { ssdesc.MaximumAnisotropy = value; modified(); } }

        /// <summary>
        /// Gets or sets the texture-address mode for the w-coordinate.
        /// </summary>
        public Microsoft.Xna.Framework.Graphics.TextureAddressMode AddressW
        {
            get { return EnumConversions.convert(ssdesc.AddressW); }
            set { ssdesc.AddressW = EnumConversions.convert(value); modified(); }
        }

        /// <summary>
        /// Gets or sets the texture-address mode for the v-coordinate.
        /// </summary>
        public Microsoft.Xna.Framework.Graphics.TextureAddressMode AddressV
        {
            get { return EnumConversions.convert(ssdesc.AddressV); }
            set { ssdesc.AddressV = EnumConversions.convert(value); modified(); }
        }

        /// <summary>
        /// Gets or sets the texture-address mode for the u-coordinate.
        /// </summary>
        public Microsoft.Xna.Framework.Graphics.TextureAddressMode AddressU
        {
            get { return EnumConversions.convert(ssdesc.AddressU); }
            set { ssdesc.AddressU = EnumConversions.convert(value); modified(); }
        }

        /// <summary>
        /// Gets or sets the type of filtering during sampling.
        /// </summary>
        public Microsoft.Xna.Framework.Graphics.TextureFilter Filter
        {
            get { return EnumConversions.convert(ssdesc.Filter); }
            set { ssdesc.Filter = EnumConversions.convert(value); modified(); }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Methods /////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Immediately releases the unmanaged resources used by this object.
        /// </summary>
        public void Dispose()
        {
            if (dxsamplerstate != null)
            {
                dxsamplerstate.Dispose();
            }
        }
    }
}
